package com.poizon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.poizon.dao.LikesDao;
import com.poizon.dao.MomentCommentDao;
import com.poizon.dao.RecordMomentDao;
import com.poizon.dto.LikeCommentDto;
import com.poizon.dto.LikeMomentDto;
import com.poizon.entity.Likes;
import com.poizon.entity.MomentComment;
import com.poizon.entity.RecordMoment;
import com.poizon.service.intf.LikeService;
import com.poizon.service.intf.MomentCommentService;
import com.poizon.service.intf.RecordMomentService;
import com.poizon.utils.BeanUtils;
import com.poizon.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ClassName LikeServiceImpl
 * @Author NoCheng
 * @Date 2022/1/5 20:41
 * @Description:TODO 点赞业务实现层
 */
@Service
public class LikeServiceImpl implements LikeService {

    private final LikesDao dao;
    private final RecordMomentDao momentDao;
    private final MomentCommentDao commentDao;

    public LikeServiceImpl(LikesDao dao, RecordMomentDao momentDao, MomentCommentDao commentDao) {
        this.dao = dao;
        this.momentDao = momentDao;
        this.commentDao = commentDao;
    }

    @Override
    @Transactional
    public  R likeMoment(LikeMomentDto dto) {
        //判断是否已经点赞
        QueryWrapper<Likes> wrapper = new QueryWrapper<>();
        wrapper.eq("userinfo_id", dto.getUserinfoId())
                .eq("moment_id", dto.getMomentId());
        Likes res = dao.selectOne(wrapper);
        UpdateWrapper<RecordMoment> momentWrapper = new UpdateWrapper<>();
        momentWrapper.eq("id", dto.getMomentId());
        if (res == null) {
            // 点赞
            Likes likes = BeanUtils.beanConvert(Likes.class, dto, dto.getClass().getDeclaredFields());
            try{
                dao.insert(likes);
            }catch (Exception e){
                throw new RuntimeException("点击过快");
            }
            //更新动态表
            momentWrapper.setSql("likes_number = likes_number + 1");
            momentDao.update(null, momentWrapper);
            return R.ok("点赞成功");
        } else {
            // 取消点赞
            if (dao.deleteById(res.getId()) > 0) {
                //更新动态表
                momentWrapper.setSql("likes_number = likes_number - 1");
                momentDao.update(null, momentWrapper);
                return R.ok("取消点赞");
            }
            throw new RuntimeException("点击过快");
        }
    }

    @Override
    public  R likeComment(LikeCommentDto dto) {
        //判断是否已经点赞
        QueryWrapper<Likes> wrapper = new QueryWrapper<>();
        wrapper.eq("userinfo_id", dto.getUserinfoId())
                .eq("comment_id", dto.getCommentId());
        Likes res = dao.selectOne(wrapper);
        UpdateWrapper<MomentComment> commentWrapper = new UpdateWrapper<>();
        commentWrapper.eq("id", dto.getCommentId());
        if (res == null) {
            //点赞
            try {
                dao.insert(BeanUtils.beanConvert(Likes.class, dto, dto.getClass().getDeclaredFields()));
            } catch (Exception e) {
                throw new RuntimeException("太快了, 休息一下吧");
            }
            //更新评论表
            commentWrapper.setSql("likes_number = likes_number + 1");
            commentDao.update(null, commentWrapper);
            return R.ok("点赞成功");
        } else {
            //取消点赞
            if (dao.deleteById(res.getId()) > 0) {
                //更新动态表
                commentWrapper.setSql("likes_number = likes_number - 1");
                commentDao.update(null, commentWrapper);
                return R.ok("取消点赞");
            }
            throw new RuntimeException("太快了, 休息一下吧");
        }
    }
}
